package com.yc.Testjvm.cglib.demo2;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class BookProxyLib implements MethodInterceptor {
	private Object target;
	
	public Object getInstance(Object target) {
		this.target = target; 
		 Enhancer enhancer = new Enhancer(); 
		 enhancer.setSuperclass(this.target.getClass()); 
		 // 回调方法 
		 enhancer.setCallback(this); 
		 // 创建代理对象 
		 return enhancer.create(); 
	}
	
	public Object getInstance2(Class clazz) {
		 Enhancer enhancer = new Enhancer(); 
		 enhancer.setSuperclass(clazz); 
		 // 回调方法 
		 enhancer.setCallback(this); 
		 // 创建代理对象 
		 return enhancer.create(); 
	}
	
	@Override
	public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
		System.out.println("代理开始-------------");
		Object result = proxy.invokeSuper(obj, args);
		System.out.println("代理结束-------------");
		return null;
	}

}
